<template>
  <div class="app-container">
    <!-- <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small">
          查询结果
        </el-button>
        <el-button
          style="float: right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
    </el-card>-->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" />
      <span>数据列表</span>
      <el-button type="primary" size="small" @click="handleAddProduct">添加新的菜品</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="dishTable" v-loading="dishListLoading" :data="dishList" border>
        <el-table-column label="菜品ID" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="图片" width="120" align="center">
          <template slot-scope="scope">
            <img style="width: 100px" :src="scope.row.photoUrl">
          </template>
        </el-table-column>
        <el-table-column label="菜品名" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="菜品价格" width="90" align="center">
          <template slot-scope="scope">{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column label="简介" align="center">
          <template slot-scope="scope">{{ scope.row.resume }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="scope">
            <p>
              热销:
              <el-switch
                v-model="scope.row.state"
                active-value="1"
                inactive-value="0"
                @change="handChangeDishState(scope.row)"
              />
            </p>
          </template>
        </el-table-column>
        <el-table-column label="销量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.saleNum }}</template>
        </el-table-column>
        <el-table-column label="库存" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.storeNum }}</template>
        </el-table-column>
        <el-table-column label="菜品分类" width="90" align="center">
          <!-- <template slot-scope="scope">{{ scope.row.dishesSeriesId }}</template> -->
          <template slot-scope="{ row }">
            {{ categoryId2Name(row.dishesSeriesId) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="{row,$index}">
            <el-button size="mini" @click="handleEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row.id, $index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getDishes, updateDishes, deleteDishes } from '@/api/dish'
import { getCategory } from '@/api/category'

export default {
  data() {
    return {
      dishList: null,
      dishListLoading: true,
      categoryList: null,
      categoryListLoading: true,
      categoryMap: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.getCategoryList()
      this.getDishList()
    },
    getCategoryList() {
      this.categoryListLoading = true
      getCategory().then(response => {
        this.categoryList = response.data
        this.categoryListLoading = false
        this.categoryMap = new Map()
        this.categoryList.forEach(element => {
          this.categoryMap.set(element.id, element.name)
        })
      })
    },
    getDishList() {
      this.dishListLoading = true
      getDishes().then(response => {
        this.dishList = response.data
        this.dishListLoading = false
      })
    },
    categoryId2Name(categoryId) {
      const name = this.categoryMap.get(categoryId)
      return name
    },
    handChangeDishState(row) {
      // 修改热销状态
      updateDishes(row).then(response => {
        this.$message.success('修改成功')
      }).catch(error => {
        if (error) {
          if (row.state === '1') {
            row.state = '0'
          } else {
            row.state = '1'
          }
        }
      })
    },
    handleAddProduct() {
      this.$router.push({ path: '/dishes/addDishes' })
    },
    handleEdit(dishId) {
      this.$router.push({ path: '/dishes/updateDishes', query: { id: dishId }})
    },
    handleDelete(dishId, index) {
      this.$confirm('是否要进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDishes(dishId).then(response => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    }
  }
}
</script>
